﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace 创业之星
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 请求
        /// </summary>
        private HttpClient httpClient;

        private bool IsStop = false;

        private int seqNum = 0;

        private string adress1 = "http://star.sznsibi.org/vote/vote_946.aspx";
        private string adress2 = "http://star.sznsibi.org/vote.ashx?id=946";

        private void FrmMain_Load(object sender, EventArgs e)
        {

            httpClient = new HttpClient(new Uri(adress1));
            httpClient.Encoding = Encoding.UTF8;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.IsStop = false;
            Thread thread = new Thread(new ThreadStart(BeginVote));
            thread.Start();
        }

        private void BeginVote()
        {
            //this.IsStop = true;
            HttpClient client = new HttpClient(new Uri(adress1));
            client.Encoding = Encoding.UTF8;

            while (!this.IsStop)
            {
                try
                {
                    string srcString = client.DownloadString(adress2);
                    this.SetText(srcString);
                }
                catch (Exception e)
                {
                    this.SetText("错误了."+e.Message);
                }
                
                if (this.cbChangeIp.Checked)
                {
                    Connect();
                }
            }
            
        }

        /// <summary>
        /// 宽带连接
        /// </summary>
        private void Connect()
        {
            try
            {
                RASDisplay ras = new RASDisplay();
                ras.Disconnect();//断开连接
                int connect = ras.Connect("宽带连接");//重新拨号
                this.SetText("执行连接方法!");
                Uri uriString = new Uri("http://www.baidu.com/");
                byte[] responseData = httpClient.DownloadData(uriString);
            }
            catch (Exception e)
            {
                 this.SetText("宽带连接错误," + e.Message);
            }

        }

        //给值
        /// <summary>
        ///  给值
        /// </summary>
        /// <param name="text"></param>
        private void SetText(string text)
        {
            if (this.rtbResult.InvokeRequired)
            {
                Action<string> set = new Action<string>(SetText);
                this.Invoke(set, text);
            }
            else
            {
                this.rtbResult.AppendText(string.Format("{0}.\t{1}\t{2} {3}", (++seqNum), DateTime.Now, text, Environment.NewLine));
                if (seqNum % 100 == 0)
                {
                    VoteLog.AddResult("MIUI.mi", this.rtbResult.Text);
                    this.rtbResult.Clear();
                }
                this.rtbResult.ScrollToCaret();

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.IsStop = true;
        }


       
    }
}
